function [fb_snips_av,pb_snips_av,fb_snips_out,pb_snips_out]=pert_ana_meta(nbr_frames,stim_id,velM_ind,velP_ind,velM_raw,ROI_act)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   mean pert (local and full field) response per cell
%   For 1 layer exp use only the first 2 arguments
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% fig_pos=[25   477   612   327];
ids=[-1:-1:-6 1:7];
l_win=10;
r_win=5;
data_lwin=5;
data_rwin=35;
shutterOpen=[];
shutterOpen(1)=0;
shutterOpen(2)=nbr_frames(1);
for ynd=2:length(nbr_frames)
    shutterOpen((ynd*2)-1)=sum(nbr_frames(1:ynd-1))+1;
    shutterOpen(ynd*2)=sum(nbr_frames(1:ynd));
end

% Code:
% 1 = feedback
% 2 = playback
% 3 = baseline

if length(nbr_frames)>5
    order=[];
    for xnd=1:length(nbr_frames)
        if nbr_frames(xnd)==5000
            if xnd==length(nbr_frames)
                order(xnd)=3;
            else
                order(xnd)=1;
            end
        else
            order(xnd)=2;
        end
    end
else
    order=[1 2 1 2 3];
end
a=1:length(order);
order(max(a(order==2))+1:end)=3;

results=struct;
results.snips={};
results.stim={};
results.runM={};
results.velM={};
results.runP={};
cnt=1;
cnt2=1;
for tnd=1:length(order)
    counter=(tnd*2)-1;
    if order(tnd)==1
        [fb_snips,fb_runM,fb_stim,fb_velM,~]=fb_calc(stim_id,velM_ind,ids,l_win,r_win,data_lwin,data_rwin,shutterOpen,counter,ROI_act,velM_raw);
        results.snips{tnd}=fb_snips;
        results.stim{tnd}=fb_stim;
        results.runM{tnd}=fb_runM;
        results.velM{tnd}=fb_velM;
        cnt=cnt+1;
    elseif order(tnd)==2
        [pb_snips,pb_runM,pb_stim,pb_runP,pb_velM,~]=pb_calc(stim_id,velM_ind,velP_ind,ids,l_win,r_win,data_lwin,data_rwin,shutterOpen,counter,ROI_act,velM_raw);
        results.snips{tnd}=pb_snips;
        results.stim{tnd}=pb_stim;
        results.runM{tnd}=pb_runM;
        results.velM{tnd}=pb_velM;
        results.runP{tnd}=pb_runP;
        cnt2=cnt2+1;
    end
end

fb_snips_av={};
pb_snips_av={};
for cnd=1:length(ids)
    fb_av=[];
    pb_av=[];
    cnt=0;
    cnt2=0;
    for mnd=1:length(results.snips)
        if order(mnd)==1
            if ~isempty(results.snips{mnd}{cnd})
                fb_av(:,cnt+1:cnt+size(results.snips{mnd}{cnd},2))=results.snips{mnd}{cnd};
                cnt=cnt+size(results.snips{mnd}{cnd},2);
            end
        elseif order(mnd)==2
            if ~isempty(results.snips{mnd}{cnd})
                pb_av(:,cnt2+1:cnt2+size(results.snips{mnd}{cnd},2))=results.snips{mnd}{cnd};
                cnt2=cnt2+size(results.snips{mnd}{cnd},2);
            end
        end
    end
    fb_snips_out{cnd}=fb_av;
    pb_snips_out{cnd}=pb_av;
    if ~isempty(fb_av)
        if size(fb_av,2)>1
            fb_snips_av{cnd}=mean(fb_av');
        else
            fb_snips_av{cnd}=fb_av';
        end
    else
        fb_snips_av{cnd}=[];
    end
    if ~isempty(pb_av)
        if size(pb_av,2)>1
            pb_snips_av{cnd}=mean(pb_av');
        else
            pb_snips_av{cnd}=pb_av';
        end
    else
        pb_snips_av{cnd}=[];
    end
end

% velM_fb_snips_av={};
% velM_pb_snips_av={};
% for cnd=1:length(ids)
%     fb_av=[];
%     pb_av=[];
%     cnt=0;
%     cnt2=0;
%     for mnd=1:length(results.velM)
%         if order(mnd)==1
%             if ~isempty(results.velM{mnd}{cnd})
%                 fb_av(:,cnt+1:cnt+size(results.velM{mnd}{cnd},2))=results.velM{mnd}{cnd};
%                 cnt=cnt+size(results.velM{mnd}{cnd},2);
%             end
%         elseif order(mnd)==2
%             if ~isempty(results.velM{mnd}{cnd})
%                 pb_av(:,cnt2+1:cnt2+size(results.velM{mnd}{cnd},2))=results.velM{mnd}{cnd};
%                 cnt2=cnt2+size(results.velM{mnd}{cnd},2);
%             end
%         end
%     end
%     velM_fb_snips_out{cnd}=fb_av;
%     velM_pb_snips_out{cnd}=pb_av;
%     if ~isempty(fb_av)
%         if size(fb_av,2)>1
%             velM_fb_snips_av{cnd}=mean(fb_av');
%         else
%             velM_fb_snips_av{cnd}=fb_av';
%         end
%     else
%         velM_fb_snips_av{cnd}=[];
%     end
%     if ~isempty(pb_av)
%         if size(pb_av,2)>1
%             velM_pb_snips_av{cnd}=mean(pb_av');
%         else
%             velM_pb_snips_av{cnd}=pb_av';
%         end
%     else
%         velM_pb_snips_av{cnd}=[];
%     end
% end
% sequ=[1 2 3 8 9 10 4 5 6 11 12 13 7];
% figure;
% for ind=1:length(ids)
%     subplot(2,7,sequ(ind))
%     rectangle('position',[15 0 10 4],'facecolor',[0.95 0.95 0.95],'linestyle','none')
%     if ~isempty(fb_snips_av{ind})
%         hold on
%         plot(fb_snips_av{ind},'color',[1 0 0],'LineWidth',2)
%     end
%     if ~isempty(pb_snips_av{ind})
%         hold on
%         plot(pb_snips_av{ind},'color',[0 0 0],'LineWidth',2)
%     end
%     axis tight
%     axis off
%     box off
%     ylim([0 curYmax])
%     xlim([0 data_rwin+data_lwin+1])
%     drawnow
% end